package com.demo.api.controller.api.dept;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.api.controller.BaseController;
import com.demo.api.service.dept.IDeptService;
import com.demo.api.vo.dept.DeptVo;
import com.demo.core.annotation.AuthToken;
import com.demo.core.response.ApiResponse;
import com.demo.core.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tan-Bowen
 * @version 1.0
 * @description TODO
 * @date 2021/7/27 14:52
 */

@Api(value = "机构管理", tags = "机构管理" )
@Slf4j
@RestController
@RequestMapping("/api/dept")
public class DeptController extends BaseController {
    @Resource
    private IDeptService deptService;

    @AuthToken(checkPower = false)
    @PostMapping("/page")
    @ApiOperation(value = "机构列表查询")
    public ApiResponse<IPage<DeptVo.PageList>> deptPage(PageUtil.Pages page, DeptVo.Select select){
        return ApiResponse.success(deptService.deptPage(getPage(page),select));
    }


    @AuthToken(checkPower = false)
    @PostMapping("/create")
    @ApiOperation(value = "机构新增")
    public ApiResponse<Void> create(@Validated DeptVo.Create request){
        deptService.create(request);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/update")
    @ApiOperation(value = "机构编辑")
    public ApiResponse<Void> update(@Validated DeptVo.Update update,
                                    @RequestHeader(value = "userId",required = false)Long userId){
        deptService.update(update);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @GetMapping("/getUserDept")
    @ApiOperation(value = "根据当前用户或都当前部门和下级部门")// 超级管理员获取的就是所有的部门
    public ApiResponse<List<DeptVo.PageList>> getUserDept(){
        return ApiResponse.success(deptService.getUserDept());
    }

    @AuthToken(checkPower = false)
    @PostMapping("/deleteDept")
    @ApiOperation(value = "部门删除")
    public ApiResponse<Void> deleteDept(@RequestParam("id")Long id){
        deptService.deleteDept(id);
        return ApiResponse.success();
    }
}